package com.yaxin.heshanghui.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.framework.Utils.Utils;
import com.yaxin.heshanghui.R;
import com.yaxin.heshanghui.activity.BiuooHomepageActivity;
import com.yaxin.heshanghui.adapter.BiuooEntity;

/**
 * activity的base类
 *
 * @author Yangjie
 *         className BiuooBaseActivity
 *         created at  2017/3/15  13:44
 */
public class BiuooBaseActivity extends AppCompatActivity {

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            Utils.isCloseSoftInputMethod(this, null, true);
            beforeFinishActivity();
            finishActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void startActivity(@Nullable Class<?> cls) {
        startActivity(cls, new Bundle());
    }

    /**
     * 返回主页，这个会clearHistoryTasks，并新建HomepageActivity
     */
    public void goBackHomepage() {
        Intent intent = new Intent(BiuooBaseActivity.this, BiuooHomepageActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        overridePendingTransition(R.anim.biuoo_slide_right_in,
                R.anim.biuoo_slide_left_out);
    }

    /**
     * 返回重新登录
     */
    public void reLogin() {
//        PreferencesHelper.getInstance().putInfo(ConstantsME.token, "");
//        startActivity(new Intent(BiuooBaseActivity.this,
//                LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
//        finish();
    }


    public void startActivity(@Nullable Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(BiuooBaseActivity.this, cls);
        if (null != bundle)
            intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition(R.anim.biuoo_slide_right_in,
                R.anim.biuoo_slide_left_out);
    }

//    public void startActivity(@Nullable Class<?> cls, BiuooEntity entity) {
//        Intent intent = new Intent(BiuooBaseActivity.this, cls);
//        if (null != entity)
//            intent.putExtra(ConstantsME.entity, entity);
//        startActivity(intent);
//        overridePendingTransition(R.anim.slide_right_in,
//                R.anim.slide_left_out);
//    }

    public void startActivityForResult(@Nullable Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(BiuooBaseActivity.this, cls);
        if (null != bundle)
            intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.biuoo_slide_right_in,
                R.anim.biuoo_slide_left_out);
    }

    public void beforeFinishActivity() {

    }

    public void finishActivity() {
        Utils.isCloseSoftInputMethod(this, null, true);
        finish();
        overridePendingTransition(R.anim.biuoo_slide_left_in,
                R.anim.biuoo_slide_right_out);
    }
}
